<template>
  <div class="warning-detail-right">
    <nanning-screen-border-box :bgImg="bgImg">
      <screen-title>查询列表</screen-title>
      <div class="warning-detail-right-content">
        <nanning-screen-search placeholder-value="请输报案人、报案内容、案件类型" :value="searchValue" @on-input-search="inputSearch" @on-click-search="onSearch"></nanning-screen-search>
        <!-- <div class="total-num">共152条数据</div> -->
        <div class="query-list">
          <div :class="['list-item', currentItem === index ? 'active' : '']" v-for="(item, index) in warningList" :key="index" @click="chooseItem(index, item.id)">
            <div class="number">编号：{{item.jqbh}}</div>
            <div class="time">时间：{{item.basj}}</div>
          </div>
          <screen-load-more :loadMoreVisible="loadMoreVisible" :loadMoreType="loadMoreType"></screen-load-more>
        </div>
      </div>
    </nanning-screen-border-box>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import EventBus from '../../../../utils/eventBus'
import { getWarningList } from '@/api/situationAwareness/warningDetail/warningDetail'
enum LoadMoreType {
  none = 'none',
  loading = 'loading',
}
@Component({
  name: 'warning-detail-right'
})
export default class WarningDetailRight extends Vue {
  private bgImg: string = require('assets/images/situational-awareness/security-left-border.png')

  private currentItem: string | number = ''

  private loadMoreVisible = false
  private loadMoreType: LoadMoreType = LoadMoreType.none

  private warningList: any = []

  private pageIndex = 1

  private pageSize = 10

  private searchValue = ''

  private listTotal = 0

  private loadStatus = true

  mounted() {
    this.warningList = []
    this.onReachBottom()
    this.getWarningList()
  }

  beforeDestroy () {
    // @ts-ignore
    document.querySelector('.query-list').removeEventListener('scroll', this.queryListFunction)
  }

  onReachBottom() {
    // @ts-ignore
    document.querySelector('.query-list').addEventListener('scroll', this.queryListFunction)
  }

  queryListFunction (e: any) {
    console.log('onReachBottom', this.pageIndex, this.loadMoreType)
    if (this.loadMoreType === LoadMoreType.none) {
      return
    }
    const { scrollHeight, scrollTop, clientHeight } = e.target
    if (clientHeight + scrollTop >= scrollHeight) {
      this.loadMoreVisible = true
      this.pageIndex++
      this.getWarningList()
    }
  }

  // 选择客运站
  chooseItem(index: number, id: string) {
    this.currentItem = index
    EventBus.$emit('chooseWarning', { id: id, index: index })
  }

  // 输入搜索
  inputSearch (e: any) {
    this.searchValue = e
    if (this.searchValue === '') {
      this.warningList = []
      this.pageIndex = 1
      this.listTotal = 0
      this.getWarningList()
    }
  }
  // 搜索
  onSearch () {
    this.warningList = []
    this.pageIndex = 1
    this.listTotal = 0
    if (this.searchValue) {
      this.getWarningList()
    }
  }
  // 获取数据列表
  async getWarningList () {
    this.loadStatus = true
    EventBus.$emit('resetMarker')
    const params = {
      dzcode: '',
      pageSize: 10,
      pageIndex: 1,
      timestamp: new Date().toLocaleString(),
      searchKey: this.searchValue,
      orgName: '',
      routeName: '',
      warningType: '',
      time: ''
    }
    try {
      this.loadMoreType = LoadMoreType.loading
      const res: any = await getWarningList(params)
      this.warningList = [...this.warningList, ...res.data]
      // this.listTotal = res.data.total
      this.loadMoreType = res.data.length < this.pageSize ? LoadMoreType.none : LoadMoreType.loading
      this.loadStatus = false
      EventBus.$emit('warningList', this.warningList)
    } catch (err) {
      this.pageIndex = 1
      this.listTotal = 0
      this.loadMoreType = LoadMoreType.none
      this.loadStatus = false
      console.error(err)
    }
  }
}
</script>

<style lang="scss" scoped>
.warning-detail-right {
  box-sizing: border-box;
  position: absolute;
  right: 0;
  top: 0;
  z-index: 1;
  width: 25.47%;
  height: 87.13%;
  animation: fadeInRight 0.7s linear;
  &-content {
    width: 100%;
    height: calc(100% - 50px);
    box-sizing: border-box;
    padding: 15px 24px 18px 22px;
    .total-num {
      width: 100%;
      height: 4em;
      line-height: 56px;
      font-size: 14px;
      font-family: 'PingFang Regular';
      font-weight: 400;
      color: rgba(255, 255, 255, 0.67);
      margin-top: 10px;
      text-indent: 20px;
    }
    .query-list {
      width: 100%;
      height: calc(100% - 42px - 10px - 56px);
      overflow-y: auto;
      &::-webkit-scrollbar {
        /*滚动条整体样式*/
        width: 6px; /*高宽分别对应横竖滚动条的尺寸*/
        height: 1px;
      }
      &::-webkit-scrollbar-thumb {
        /*滚动条里面小方块*/
        border-radius: 10px;
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        background: #114da7;
      }
      &::-webkit-scrollbar-track {
        /*滚动条里面轨道*/
        box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        border-radius: 10px;
        background: #061836;
      }
      .list-item {
        width: 100%;
        box-sizing: border-box;
        padding: 15px 20px;
        // background: rgba(16, 75, 164, 0.22);
        border-radius: 2px;
        cursor: pointer;
        border-bottom: 1px solid rgba($color: #3C5FAE, $alpha: 1);
        .number {
          font-size: 18px;
          font-family: 'PingFang Regular';
          font-weight: 400;
          color: rgba($color: #ffffff, $alpha: 1.0);
          width: 100%;
          overflow: hidden;
          text-overflow:ellipsis;
          white-space: nowrap;
          margin-bottom: 10px;
        }
        .time {
          font-size: 16px;
          font-family: 'PingFang Regular';
          font-weight: 400;
          color: rgba($color: #ffffff, $alpha: 0.7);
        }
        & + .list-item {
          // margin-top: 6px;
        }
        &.active {
          background: rgba($color: #104BA4, $alpha: 0.22);
        }
      }
    }
  }
}
</style>
